﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BL;
using DAL;

namespace WebsiteBanHang
{
    public partial class SuaSanPham : System.Web.UI.Page
    {
        NhaSanXuatBL nhaSanXuat = new NhaSanXuatBL();
        DanhMucLoaiSanPhamBL dmloaiSanPham = new DanhMucLoaiSanPhamBL();
        SanPhamDAL sp = new SanPhamDAL();
        SanPhamBL sanPham = new SanPhamBL();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            LayDSNhaSanXuat();
            LayDMLoaiSanPham();
            //TH=-----------------------------
            string maSP = Request.QueryString["MaSanPham"];
            sp = sanPham.LaySanPhamTheoMaSP(maSP);
            txtMaSP.Text = sp.MaSanPham.ToString();
            txtTenSP.Text = sp.TenSanPham.ToString();
            Image1.ImageUrl = "../images/" + sp.HinhAnh;           
            ddlloaiSanPham.Text = sp.MaLoaiSanPham.ToString();
            ddlnhaSanXuat.Text = sp.MaNhaSanXuat.ToString();

            txtGiaBan.Text = sp.GiaBan.ToString();
            txtSoLuong.Text = sp.SoLuongTon.ToString();            
            rdiKMmai.Text = sp.PhanTramKM.ToString();
            


        }
        protected void LayDSNhaSanXuat()
        {
            List<NhaSanXuatDAL> nsx = new List<NhaSanXuatDAL>();
            nsx = nhaSanXuat.LayDanhSachNhaSanXuat();

            for (int i = 0; i < nsx.Count; ++i)
            {
                ddlnhaSanXuat.Items.Add(nsx[i].TenNhaSanXuat);
                ddlnhaSanXuat.Items[i].Value = nsx[i].MaNhaSanXuat;
                ddlnhaSanXuat.Items[i].Text = nsx[i].TenNhaSanXuat;
            }
        }

        protected void LayDMLoaiSanPham()
        {
            List<DanhMucLoaiSanPhamDAL> dmlsp = new List<DanhMucLoaiSanPhamDAL>();
            dmlsp = dmloaiSanPham.LayDanhMucLoaiSanPham();

            for (int i = 0; i < dmlsp.Count; ++i)
            {
                ddlloaiSanPham.Items.Add(dmlsp[i].TenLoaiSanPham);
                ddlloaiSanPham.Items[i].Value = dmlsp[i].MaLoaiSanPham;
                ddlloaiSanPham.Items[i].Text = dmlsp[i].TenLoaiSanPham;
            }
        }

        protected void btnSuaSP_Click(object sender, EventArgs e)
        {
            //SanPhamDAL sp = new SanPhamDAL();
            //sp.MaSanPham = TextBox1.Text;
            //sp.TenSanPham = TextBox2.Text;
            //sp.HinhAnh = /*"../images/" +*/ FileUpload1.FileName;
            string maSP = Request.QueryString["MaSanPham"];
            sp.MaSanPham = maSP;
            sp.TenSanPham = txtTenSP.Text;
            sp.HinhAnh =/*"../images/" +*/ FileUpload1.FileName;
            sp.MaNhaSanXuat = ddlnhaSanXuat.SelectedValue;
            sp.MaLoaiSanPham = ddlloaiSanPham.SelectedValue;
            sp.GiaBan = float.Parse(txtGiaBan.Text);
            sp.PhanTramKM = float.Parse(rdiKMmai.SelectedValue);
            sp.SoLuongTon = int.Parse(txtSoLuong.Text);
            //sp.ThongSoKyThuat = null;
            sp.GiaMua = 0;
            sanPham.SuaSP(sp);
            //sp.MaNhaSanXuat = ddlnhaSanXuat.SelectedValue;
            //sp.MaLoaiSanPham = ddlloaiSanPham.SelectedValue;

            //sp.GiaBan = float.Parse(TextBox3.Text);
            //sp.GiaMua = 0;
            //sp.PhanTramKM = float.Parse(RadioButtonList1.SelectedValue);
            //sp.SoLuongTon = int.Parse(TextBox4.Text);
            //sp.ThongSoKyThuat = null;
            //sp.TinhTrang = true;
            //sp.SanPhamMoi = true;
            //SanPhamBL sanPham = new SanPhamBL();
            //sanPham.ThemSanPhamMoi(sp);

            //TextBox1.Text = "";
            //TextBox2.Text = "";
            //ddlnhaSanXuat.Items.Clear();
            //ddlloaiSanPham.Items.Clear();
            //TextBox3.Text = "";
            //TextBox4.Text = "";
            //RadioButtonList1.SelectedIndex = -1;
            //LayDMLoaiSanPham();
            //LayDSNhaSanXuat();
            
        }

    }
}